<?php

class emailResource extends resource{

    private $mailer = NULL;
    public $dir = "";
    public function __contruct() {
        $this->dir = dirname(__FILE__);
        parent::__contruct();
    }
    
    public function load(){
        $this->dir = dirname(__FILE__);
        $this->LoadResourceFile("/classes/mailer.php");
        $this->LoadResourceFile("/lib/mailer/phpmailer/phpmailerMailer.php");
        $this->mailer = phpmailerMailer::getInstanceOf();
    }
    
    private static $instance = NULL;
    public static function getInstanceOf(){
        $class_name = __CLASS__;
        if (!isset(self::$instance)){
            $obj = new $class_name();
            $obj->load();
            self::$instance = $obj;
        }
        return self::$instance;
    } 
    
    public function configure($assunto, $corpo, $email_remetente, $nomeRemetente){
        $this->mailer->configure($assunto, $corpo, $email_remetente, $nomeRemetente);
    }

    public function AddAddress($destinatario){
        $this->mailer->AddAddress($destinatario);
    }

    public function send(){
        if(!$this->mailer->send()){
            $this->setErrorMessage($this->mailer->getErrorMessage());
            return false;
        }
        return true;
    }
    
    public function AddAtachment($anexo){
        if(!empty ($anexo)){
            foreach($anexo as $an){
                $this->mailer->AddAttachment($an['tmp_name'], $an['name']);
            }
        }
    }
    
    public function privacidade(){
        $this->mailer->privacidade();
    }

    public function sendMail(
            $assunto, $corpo, $email_remetente = "", 
            $destinatarios = "", $nomeRemetente="", $anexo = array()
    ){
        $this->configure($assunto, $corpo, $email_remetente, $nomeRemetente);
        $this->AddAtachment($anexo);
        $this->AddAddress($destinatarios);
        return $this->send();
    }
	
}

?>